package com.qianfeng.smsplatform.search.utils;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;

/**
 * Created by jackiechan on 2020-03-02 16:02
 *
 * @Author jackiechan
 */
public class SearchUtil {

    public static void buildingMapping(CreateIndexRequest createIndexRequest, String typeName) throws IOException {
        XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")


                .startObject("corpName")
                .field("type", "keyword")
                .endObject()


                .startObject("createDate")
                .field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss")
                .endObject()

                .startObject("fee")
                .field("type", "integer")
                .endObject()

                .startObject("ipAddr")
                .field("type", "ip")
                .endObject()


                .startObject("longCode")
                .field("type", "keyword")
                .endObject()

                .startObject("mobile")
                .field("type", "keyword")
                .endObject()

                .startObject("province")
                .field("type", "keyword")
                .endObject()

                .startObject("replyTotal")
                .field("type", "integer")
                .endObject()

                .startObject("operatorId")
                .field("type", "integer")
                .endObject()

                .startObject("smsContent")
                .field("type", "text")
                .field("analyzer", "ik_max_word")
                .endObject()

                .startObject("state")
                .field("type", "integer")
                .endObject()

                .startObject("sendDate")
                .field("type", "date")
                .field("format", "yyyy-MM-dd HH:mm:ss")
                .endObject()
                .endObject()
                .endObject();


        createIndexRequest.mapping(typeName, xContentBuilder);

    }

    /**
     * 设置 index 的分片规则
     *
     * @param createIndexRequest
     */
    public static void buildingSetting(CreateIndexRequest createIndexRequest) {
        createIndexRequest.settings(Settings.builder()//
                .put("number_of_shards", 3)// 设置主分片为 3
                .put("number_of_replicas", 2));//设置从分片为 2
    }


    public static SearchSourceBuilder builSearachSource(Map<String, Object> params) {
        String mobile = (String) params.get("mobile");
        String clietnID =params.get("clientID")==null?null:params.get("clientID").toString();
        Long startTime = (Long) params.get("startTime");
        Long endTime = (Long) params.get("endTime");
        String keyword = (String) params.get("keyword");//关键字

        TermQueryBuilder mobileSearch = null;
        TermQueryBuilder clietnIDSearch = null;
        RangeQueryBuilder timeSearch = null;
        MatchQueryBuilder keywordSearch = null;

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        if (!StringUtils.isEmpty(mobile)) {
            mobileSearch = QueryBuilders.termQuery("destMobile", mobile);
        }
        if (!StringUtils.isEmpty(clietnID)) {
            clietnIDSearch = QueryBuilders.termQuery("clientID", clietnID);
        }

        if (!StringUtils.isEmpty(keyword)) {
            keywordSearch = QueryBuilders.matchQuery("messageContent", keyword);
        }

        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            timeSearch = QueryBuilders.rangeQuery("sendTime").from(startTime).to(endTime);
        } else if (StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            timeSearch = QueryBuilders.rangeQuery("sendTime").lte(endTime);
        } else if (!StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)) {
            timeSearch = QueryBuilders.rangeQuery("sendTime").gte(startTime);
        }
        //用于包装查询条件的 boolean 查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if (mobileSearch != null) {
            boolQueryBuilder.must(mobileSearch);
        }

        if (clietnIDSearch != null) {
            boolQueryBuilder.must(clietnIDSearch);
        }
        if (timeSearch != null) {
            boolQueryBuilder.must(timeSearch);
        }
        if (keywordSearch != null) {
            boolQueryBuilder.must(keywordSearch);
        }

        searchSourceBuilder.query(boolQueryBuilder);

        return searchSourceBuilder;
    }
}
